package com.sphd.beautysalon.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.beautysalon.activitys.manager.DianzhangListActivity;
import com.sphd.beautysalon.adapters.SupperMeangerAdapter;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.LoginInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.presenter.SupperMeangerPresenterImpl;
import com.sphd.beautysalon.utils.AppManager;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.view.SupperMeangerView;
import com.sphd.beautysalon.R;

import org.litepal.crud.DataSupport;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 超管界面
 */
public class SupperMeangerActivity extends BaseActivity implements SupperMeangerView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.daymoney_textview)
    TextView daymoneyTextview;
    @BindView(R.id.monthmoney_textview)
    TextView monthmoneyTextview;
    @BindView(R.id.allmoney_textview)
    TextView allmoneyTextview;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    SupperMeangerAdapter supperMeangerAdapter;
    LoginInfo loginInfo;
    @BindView(R.id.action_dianzhang)
    ImageView actionDianzhang;
    private SupperMeangerPresenterImpl supperPresenter;
    private List<RestaurantInfo> restaurantInfoList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_supper_meanger);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(() -> addData());
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                Bundle bundle = new Bundle();
                bundle.putSerializable(Constant.RESTAURANTSINFO, (Serializable) baseQuickAdapter.getItem(i));


                Intent intent = new Intent();
                intent.putExtras(bundle);
                intent.putExtra(Constant.TAG, context.getClass().getSimpleName());
                intent.setClass(context, UserHomeActivity.class);
                startActivityForResult(intent,1);

                toast(((RestaurantInfo)baseQuickAdapter.getItem(i)).getCg_name());
            }
        });
    }

    private void initView() {
        setTitleBarNoBack(toolbar, "我的店铺");
        loginInfo = DataSupport.findLast(LoginInfo.class);
        supperPresenter = new SupperMeangerPresenterImpl(this);
        setRefresh(mySwf);
        setRecy(myRecy);
        supperMeangerAdapter = new SupperMeangerAdapter(R.layout.title_time, restaurantInfoList);
        myRecy.setAdapter(supperMeangerAdapter);
        addData();
    }

    /**
     * 获取数据信息
     */
    private void addData() {

        supperPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.RestaurantList + "superid=" + loginInfo.getSuperid());
    }

    /**
     * @param all 累计销售额
     */
    @Override
    public void setAll(double all) {
        allmoneyTextview.setText("累计销售额\n￥" + turn(all));
    }

    /**
     * @param month 本月销售额
     */
    @Override
    public void setMonth(double month) {
        monthmoneyTextview.setText("本月销售额\n￥" + turn(month));
    }

    /**
     * @param today 今日销售额
     */
    @Override
    public void setToday(double today) {
        daymoneyTextview.setText("今日销售额\n￥" + turn(today));
    }

    @Override
    public void setResList(List<RestaurantInfo> restaurantInfos) {
        supperMeangerAdapter.setNewData(restaurantInfos);
    }

    /**
     * 店长列表
     */
    @OnClick(R.id.action_dianzhang)
    public void onClick() {
        startActivity(DianzhangListActivity.class);
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
        mySwf.setRefreshing(false);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==0){
            addData();
        }
    }
}
